﻿/****************************** Module Header ******************************\
 * Module Name:  mxMdiForm.cs
 * Module Version:  1.0.0 (2012-8-9)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used for mxMdiForm. 
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace UCL.Forms
{
    /// <summary>
    /// Mdi父窗体扩展方法
    /// </summary>
    public static class mxMdiForm
    {
        #region 私有字段        
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int GetWindowLong(IntPtr handle, int nIndex);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int SetWindowLong(IntPtr handle, int nIndex, int dwNewLong);
        private const int GWL_STYLE = -16;
        private const int GWL_EXSTYLE = -20;
        private const int WS_BORDER = 0x00800000;
        private const int WS_EX_CLIENTEDGE = 0x00000200;
        #endregion

        /// <summary>
        /// 设置Mdi窗口客户区边框样式
        /// </summary>
        /// <param name="frm">父窗体</param>
        /// <param name="border">边框样式</param>
        public static void SetMdiClientBorder(this Form frm, BorderStyle border)
        {
            MdiClient mc = null;
            foreach (Control c in frm.Controls)
            {
                if (c is MdiClient)
                {
                    mc = (MdiClient)c;
                    break;
                }
            }
            if (mc == null) return;
            int style = GetWindowLong(mc.Handle, GWL_STYLE);
            int exStyle = GetWindowLong(mc.Handle, GWL_EXSTYLE);

            switch (border)
            {
                case BorderStyle.Fixed3D:
                    exStyle |= WS_EX_CLIENTEDGE;
                    style &= ~WS_BORDER;
                    break;

                case BorderStyle.FixedSingle:
                    exStyle &= ~WS_EX_CLIENTEDGE;
                    style |= WS_BORDER;
                    break;

                case BorderStyle.None:
                    style &= ~WS_BORDER;
                    exStyle &= ~WS_EX_CLIENTEDGE;
                    break;
            }
            SetWindowLong(mc.Handle, GWL_STYLE, style);
            SetWindowLong(mc.Handle, GWL_EXSTYLE, exStyle);
        }
    }
}
